package com.zmn.oms.third.gomegj;

import com.aliyun.openservices.shade.io.netty.util.internal.StringUtil;
import com.zmn.oms.common.utils.HttpUtil;
import com.zmn.oms.third.gomegj.api.GmgjGetTokenRequest;
import com.zmn.oms.third.gomegj.dto.GmgjRequestDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Objects;

/**
 * 类描述：生成签名、验证签名以及加密等工具
 *
 * @author xiewenbing
 * @date 2020/11/04 18:30
 */
@Slf4j
public class GmgjUtil {

    /**
     * 验证订单消息的签名
     *
     * @param gmgjRequestDTO
     * @return
     */
    public static boolean verfiyOrderRequestSign(GmgjRequestDTO gmgjRequestDTO) {

        if (Objects.isNull(gmgjRequestDTO)) {
            return false;
        }

        if (StringUtil.isNullOrEmpty(gmgjRequestDTO.getSign())) {
            return false;
        }

        StringBuilder sbSign = new StringBuilder();
        sbSign.append(GmgjConsts.APP_ID).append(gmgjRequestDTO.getMsgId()).append(gmgjRequestDTO.getMethod());
        sbSign.append(gmgjRequestDTO.getBody());

        String calcSign = DigestUtils.md5Hex(sbSign.toString());

        return calcSign.compareToIgnoreCase(gmgjRequestDTO.getSign()) == 0 ? true : false;
    }

    /**
     * 生成获取token的签名
     *
     * @param getTokenRequest
     */
    public static void calcGetAccessTokenSign(GmgjGetTokenRequest getTokenRequest) {

        if (Objects.isNull(getTokenRequest)) {
            return;
        }

        StringBuilder sbSign = new StringBuilder();
        sbSign.append(GmgjConsts.APP_SECRET).append(getTokenRequest.getTimestamp()).append(getTokenRequest.getClientId());
        sbSign.append(getTokenRequest.getUsername()).append(getTokenRequest.getPassword());
        sbSign.append("access_token").append(GmgjConsts.APP_SECRET);

        getTokenRequest.setSign(DigestUtils.md5Hex(sbSign.toString()));
    }

    /**
     * post http 请求
     *
     * @param url
     * @param content
     * @return
     */
    public static String postHttpRequest(String url, String content) {

        if (StringUtils.isBlank(url)) {
            log.info("【{}】发送请求失败，URL参数不能为空",GmgjConsts.CHANNEL_NAME);
            return null;
        }

        log.info("【{}】发送请求，URL【{}】,内容【{}】",GmgjConsts.CHANNEL_NAME,url,content);
        String response = "";
        for (int i=0; i<3; i++) {
            try {

                if (url.matches("https(.*)")) {
                    response = HttpUtil.httpsPost(url, content);
                } else {
                    response = HttpUtil.httpPost(url, content);
                }

                if (!StringUtil.isNullOrEmpty(response)) {
                    break;
                }
            } catch (Exception exception) {
                log.info("【{}】发送请求失败,{}",GmgjConsts.CHANNEL_NAME,Objects.nonNull(exception)? exception.getMessage():"网络请求异常");
            }
        }

        log.info("【{}】发送请求应答【{}】",GmgjConsts.CHANNEL_NAME,response);
        return response;
    }

    /**
     * get http 请求
     *
     * @param url
     * @return
     */
    public static String getHttpRequest(String url) {

        if (StringUtils.isBlank(url)) {
            log.info("【{}】发送HTTP请求失败，URL参数不能为空",GmgjConsts.CHANNEL_NAME);
            return null;
        }

        log.info("【{}】发送HTTP请求，URL【{}】",GmgjConsts.CHANNEL_NAME,url);
        String response = "";
        for (int i=0; i<3; i++) {
            try {

                if (url.matches("https(.*)")) {
                    response = HttpUtil.httpsGet(url);
                } else {
                    response = HttpUtil.httpGet(url);
                }

                if (!StringUtil.isNullOrEmpty(response)) {
                    break;
                }
            } catch (Exception exception) {
                log.info("【{}】发送HTTP请求异常,{}",GmgjConsts.CHANNEL_NAME,Objects.nonNull(exception)? exception.getMessage():"网络请求异常");
            }
        }

        log.info("【{}】发送HTTP请求应答【{}】",GmgjConsts.CHANNEL_NAME,response);
        return response;
    }
}
